package main

import (
	"fmt"
	"sync"
)

//需求：定义一个全局变量num，开启1000个协程使num++
var (
	num int
	wg  sync.WaitGroup
	mtx sync.Mutex
	//mp = map[string]string{}
)

//
func add() {
	defer wg.Done()
	//加锁
	mtx.Lock()
	num++
	//mp ["name"] = "zhangsan"
	//释放锁
	mtx.Unlock()
}

func main() {
	for i := 0; i < 1000; i++ {
		wg.Add(1)
		go add()
	}
	wg.Wait()
	fmt.Println("num:", num)
}
